﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SDT.CrawlSystem.Servers.Filters
{
   public class ImgDes
    {
       public string UrlImage { get; set; }
       public string BaseURL { get; set; }
       public string Href { get; set; }
       private string RegExprIMG = @"(?<=img\s+([^>]+\s+)?src\=[\x27\x22])(?<1>[^\x27\x22]*)(?=[\x27\x22])";

       #region Constructor

       public ImgDes()
       {
          
       }
       public ImgDes(string baseURL)
       {
           BaseURL = baseURL;
       }

       public ImgDes(string baseURL, string urlImg)
       {
           BaseURL = baseURL;
           UrlImage = urlImg;

       }

       public ImgDes(string href, string baseURL, string urlImg)
       {
           BaseURL = baseURL;
           UrlImage = urlImg;
           Href = href;

       }
       #endregion

       #region Get List Image
    
       /// <summary>
       /// Ex : baseURL http://www.xxx.com
       /// Case 1 : url =/image/pic.jpg Result: http://www.xxx.com/image/pic.jpg
       /// Case 2 : url = http://vnexpress.net/image/pic.jpg Result: http://vnexpress.net/image/pic.jpg
       /// </summary>
       /// <param name="url"></param>
       /// <param name="baseUrl"></param>
       /// <returns></returns>
       private  string ConvertToAbsoluteUrl(string url, string baseUrl)
       {
           // we try to prevent the exception caused in the case the url is relative
           // (no scheme info) just for speed
           if (url.IndexOf(Uri.SchemeDelimiter) < 0 && baseUrl != null)
           {
               try
               {
                   Uri baseUri = new Uri(baseUrl);
                   return new Uri(baseUri, url).OriginalString;
               }
               catch
               {
                   return null;
               }
           }

           try
           {
               Uri uri = new Uri(url);
               return uri.OriginalString;
           }
           catch (Exception)
           {
               if (baseUrl != null)
               {
                   try
                   {
                       Uri baseUri = new Uri(baseUrl);
                       return new Uri(baseUri, url).AbsolutePath;
                   }
                   catch (Exception)
                   {
                       return null;
                   }
               }
               else
               {
                   return null;
               }
           }
       }

       public  IList<string> GetUrlAttributeSrc(string baseUrl, string html)
       {
           Regex regExpr = new Regex(RegExprIMG);
           var l = new List<string>();

           for (Match m = regExpr.Match(html); m.Success; m = m.NextMatch())
           {
               string url = m.Groups[1].ToString();
               if (!url.Contains("http:"))
               {
                   try
                   {
                       url = ConvertToAbsoluteUrl(url, baseUrl);
                   }
                   catch (Exception)
                   {
                   }
               }

               if (url != null)
               {
                   l.Add(url);
               }
           }
           return l;
       }

       public IList<string> GetUrlAttributeSrc(string html)
       {
           Regex regExpr = new Regex(RegExprIMG);
           var l = new List<string>();

           for (Match m = regExpr.Match(html); m.Success; m = m.NextMatch())
           {
               string url = m.Groups[1].ToString();
               
               if (url != null)
               {
                   l.Add(url);
               }
           }
           return l;
       }

       public  string GetUrlSrc(string baseUrl, string html)
       {
           Regex regExpr = new Regex(RegExprIMG);

           for (Match m = regExpr.Match(html); m.Success; m = m.NextMatch())
           {
               string url = m.Groups[1].ToString();

               try
               {
                   url = ConvertToAbsoluteUrl(url, baseUrl);
               }
               catch (Exception)
               {
               }

               if (url != null)
               {
                   return url;
               }
           }
           return string.Empty;
       }
       private  IList<string> GetUrlAttributeSrc(Regex regExpr, string baseUrl, string html)
       {
           var l = new List<string>();

           for (Match m = regExpr.Match(html); m.Success; m = m.NextMatch())
           {
               string url = m.Groups[1].ToString();

               try
               {
                   url = ConvertToAbsoluteUrl(url, baseUrl);
               }
               catch (Exception)
               {
               }

               if (url != null)
               {
                   l.Add(url);
               }
           }
           return l;
       }

       public string GenNameImage(string url)
       {
           string[] ExtArray = { ".gif", ".jpg", ".doc", ".zip", ".pdf" };

           foreach (string ext in ExtArray)
               if (url.ToLower().EndsWith(ext) == true)
                   return string.Empty;

           string sName = DateTime.Now.ToLongDateString();
           return sName;

       }


        #endregion

    }
}
